class Solution
{
public:
    vector<vector<string>> groupAnagrams(vector<string> &strs)
    {
        unordered_map<string, vector<int>> groups;
        for (int i = 0; i < strs.size(); ++i)
        {
            string s = strs[i];
            sort(s.begin(), s.end());
            groups[s].push_back(i);
        }
        vector<vector<string>> result;
        for (auto &[_, group] : groups)
        {
            result.push_back({});
            for (int id : group)
            {
                result.back().push_back(strs[id]);
            }
        }
        return result;
    }
};